/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MyPhotoIndex.Controls
{
    public partial class FadingForm : Form
    {
        private System.Windows.Forms.Timer  m_fadeTimer;
        private bool                        m_fadeInTag;
        private DialogResult                m_dialogResult;
        private bool                        m_closeOnFade;
        private double                      m_minOpacity;

        public FadingForm()
        {
            InitializeComponent();
            m_fadeTimer = new System.Windows.Forms.Timer();
            m_fadeTimer.Tick += new EventHandler(m_fadeTimer_Tick);
        }

        private void FadingForm_Load(object sender, EventArgs e)
        {
            Opacity = 0;
            m_fadeInTag = true;
            m_fadeTimer.Enabled = true;
        }

        protected double MinOpacity
        {
            get { return m_minOpacity; }
            set { m_minOpacity = value; }
        }

        protected void Fade(bool fade, bool close)
        {
            m_fadeInTag = !fade;
            m_closeOnFade = close;

            if (m_fadeTimer.Enabled == false)
            {
                m_fadeTimer.Enabled = true;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (e.Cancel == true)
            {
                return;
            }

            m_dialogResult = this.DialogResult;

            if (Opacity > 0)
            {
                Fade(true, true);
                e.Cancel = true;
            }
        }

        void m_fadeTimer_Tick(object sender, EventArgs e)
        {
            if (m_fadeInTag == false)
            {
                if (this.Opacity > m_minOpacity)
                {
                    Opacity -= 0.2;
                }
                m_fadeTimer.Enabled = (this.Opacity > m_minOpacity);
                if (this.Opacity <= m_minOpacity &&
                    m_closeOnFade == true)
                {
                    this.DialogResult = m_dialogResult;
                    Close();
                }
            }
            else
            {
                try
                {
                    Opacity += 0.2;
                }
                catch { }
                m_fadeTimer.Enabled = (Opacity < 1.0);
            }
        }
    }
}